import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.lang.Math;
import java.awt.*;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;


public class Interface extends JFrame{
//TODO remove tester
	private static JLabel[][] value = new JLabel[7][7];
	
//TODO end Tester
	private int offsetX = 500;	 		//offset in frame in X direction
	private int offsetY = 60;			//offset in frame in Y direction
	private int buttonNumber = 0;		//total button number
	private int dimX = 80;				//button width
	private int dimY = 50;				//button height
	private int totalPegsAllowed = 18;	//number of pegs allowed, original game 18
	private int count = 0;				//
	private int player = 1;				//keeps count of what player turn, by default player1 = black, player2 = white
	private int row = 0;				//holds row-coordinate, DO NOT RESUE
	private int col = 0;				//holds col-coordinate, DO NOT REUSE
	private int blackPegs = 0;			//counts black pegs
	private int whitePegs = 0;			//counts white pegs
	private int phase = 1;				//keeps track of the game phases, phase 1 = setup phase, phase 2 = moving phase, phase 3 = winning phase
	private int lastPeg = 0;			//holds the last peg value
	private int oldX = -1;				//keeps variable of last X pos
	private int oldY = -1;				//keeps variable of last Y pos
	private int oldRow = -1;			//last row
	private int oldCol = -1;
	private int lastRow = -1;			//last col
	private int lastCol = -1;
	private final int fly = 3;			//condition to be able to 'fly' with pegs
	private final int loose = 2;		//condition to loose
	private int maxWhitePegs = 9;	//maximum number of white pegs
	private int maxBlackPegs = 9; //maximum number of black pegs
	private final int pegSize = 50;		//size for the peg
	private boolean placeFlag = true;	//if flag is high-->player can place peg
	private boolean removePeg = false;	//if flag is high playerX can remove peg from playerY
	private boolean skip = false;		//if high skip a mouseclick
	private boolean first = true;		//
	private boolean drag = false;		//enable peg dragging
	private boolean movePeg = false;	//if false no move has been made		
	private MouseEvent mouse;
	
	private JLabel board;	//stores the image of the board in a JLabel
	private Insets insets; 	//saves insets values of the interface frame
	
	//buttons
	private JButton save;
	private JButton load;
	private JButton exit;
	private JButton reset;
	private JButton undo;
	
	private JLabel nextPlayerLabel;
	private JLabel errorLabel;
	
	//keeps track of every position and peg on the board
	//0 = empty, 1 = whitePeg, -1 = blackPeg
	static int[][] position = new int[7][7];
	static void initialPosition() {
		for (int j = 0; j < 7; j++) {
			for (int k= 0; k < 7; k++ )
				position[j][k] = 0;
		}
	}
	
	//keeps track of all mills on the board
	//initially there are no mills on the board
	//2 dimensions needed for row 3, with 2 mill possibilities
	static boolean[][] rowMill = new boolean[7][2];
	static boolean[][] colMill = new boolean[7][2];
	static void initialMill(){
		for(int i = 0; i < 7; i++){
			for(int j = 0; j < 2; j++){
				rowMill[i][j] = false;
				colMill[i][j] = false;
			}
		}
	}
	public Interface(){
		super("Nine Mens Morris");
		
		//default layout
		//no layout -- > place all elements manually
		//have to make layout scalable --> use variables
		setLayout(null);
		
		//get the dimensions of the JFrame to place elements at fixed positions
		insets = getInsets();
		
		//add the board picture to the frame
		ImageIcon img = new ImageIcon("src/image.jpg");
		int ycord = img.getIconHeight();
		int xcord = img.getIconWidth();
		
		board = new JLabel(img);
		board.setBounds(insets.right, insets.top,xcord, ycord);
		add(board);
		
		BoardHandler bHandler = new BoardHandler();
		board.addMouseListener(bHandler);
		board.addMouseMotionListener(bHandler);

		nextPlayerLabel = new JLabel("NEXT PLAYER: " + getPlayer());
		nextPlayerLabel.setBounds(insets.left, board.getHeight() + 50, 200, 50);
		add(nextPlayerLabel);
		
		errorLabel = new JLabel("TODO");
		errorLabel.setBounds(insets.left, board.getHeight() + 100, 500, 50);
		add(errorLabel);
		
		//add button handlers here
		ExitHandler eHandler = new ExitHandler();
		LoadHandler lHandler = new LoadHandler();
		SaveHandler sHandler = new SaveHandler();
		UndoHandler uHandler = new UndoHandler();
		ResetHandler rHandler = new ResetHandler();
		
		//add buttons here
		//addButton(save, "save", sHandler);
		//addButton(load, "load", lHandler);
		addButton(reset,"reset", rHandler);
		//addButton(undo, "undo",uHandler);
		addButton(exit, "exit", eHandler);
		
//TODO remove tester
		for(int i = 0; i < 7; i++){
			for(int j = 0; j < 7; j++){
				value[i][j] = new JLabel(String.format("%s",i*j));
				value[i][j].setBounds(insets.right + 700 + j *20, insets.top+ i * 20 , 20, 20);
				add(value[i][j]);
			}
		}
//TODO end tester
	}
	
	//checks for mills in rows
	private boolean rowMillUpdate(){
		int sum = 0;
		for(int i = 0; i < 3; i++){
			for(int j = 0; j < 7;j++){
				sum = sum + position[i][j];
			}
			if((sum == 3 || sum == -3) && !rowMill[i][0]){
				rowMill[i][0] = true;
				removePeg = true;
			}
			sum = 0;
		}
		sum = 0;
		for(int i = 0; i < 3; i++){
			sum = sum + position[3][i];
		}
		if((sum == 3 || sum == -3) && !rowMill[3][0]){
			rowMill[3][0] = true;
			removePeg = true;
		}
		sum = 0;
		for(int i = 4; i < 7; i++){
			sum = sum + position[3][i];
		}
		if((sum == 3 || sum == -3) && !rowMill[3][1]){
			rowMill[3][1] = true;
			removePeg = true;
		}
		
		sum = 0;
		for(int i = 4; i < 7; i++){
			for(int j = 0; j < 7;j++){
				sum = sum + position[i][j];
			}
			if((sum == 3 || sum == -3) && !rowMill[i][0]){
				rowMill[i][0] = true;
				removePeg = true;
			}
			sum = 0;
		}
		//find no mill
		sum = 0;
		for(int i = 0; i < 3; i++){
			for(int j = 0; j < 7;j++){
				sum = sum + position[i][j];
			}
			if((sum != 3 && sum != -3) && rowMill[i][0]){
				rowMill[i][0] = false;
			}
			sum = 0;
		}
		sum = 0;
		for(int i = 0; i < 3; i++){
			sum = sum + position[3][i];
		}
		if((sum != 3 && sum != -3) && rowMill[3][0]){
			rowMill[3][0] = false;
		}
		sum = 0;
		for(int i = 4; i < 7; i++){
			sum = sum + position[3][i];
		}
		if((sum != 3 && sum != -3) && rowMill[3][0]){
			rowMill[3][1] = false;
		}
		
		sum = 0;
		for(int i = 4; i < 7; i++){
			for(int j = 0; j < 7;j++){
				sum = sum + position[i][j];
			}
			if((sum != 3 && sum != -3) && rowMill[i][0]){
				rowMill[i][0] = false;
			}
			sum = 0;
		}
		return removePeg;
	}
	//checks for mills in cols
	private boolean colMillUpdate(){
			int sum = 0;
			//find mill
			for(int i = 0; i < 3; i++){
				for(int j = 0; j < 7;j++){
					sum = sum + position[j][i];
				}
				if((sum == 3 || sum == -3) && !colMill[i][0]){
					colMill[i][0] = true;
					removePeg = true;
				}
				sum = 0;
			}
			sum = 0;
			for(int i = 0; i < 3; i++){
				sum = sum + position[i][3];
			}
			if((sum == 3 || sum == -3) && !colMill[3][0]){
				colMill[3][0] = true;
				removePeg = true;
			}
			sum = 0;
			for(int i = 4; i < 7; i++){
				sum = sum + position[i][3];
			}
			if((sum == 3 || sum == -3) && !colMill[3][1]){
				colMill[3][1] = true;
				removePeg = true;
			}
			sum = 0;
			for(int i = 4; i < 7; i++){
				for(int j = 0; j < 7;j++){
					sum = sum + position[j][i];
				}
				if((sum == 3 || sum == -3) && !colMill[i][0]){
					colMill[i][0] = true;
					removePeg = true;
				}
				sum = 0;
			}
			//find no mill
			for(int i = 0; i < 3; i++){
				for(int j = 0; j < 7;j++){
					sum = sum + position[j][i];
				}
				if((sum != 3 && sum != -3) && colMill[i][0]){
					colMill[i][0] = false;
				}
				sum = 0;
			}
			sum = 0;
			for(int i = 0; i < 3; i++){
				sum = sum + position[i][3];
			}
			if((sum != 3 && sum != -3) && colMill[3][0]){
					colMill[3][0] = false;
				}
			sum = 0;
			for(int i = 4; i < 7; i++){
				sum = sum + position[i][3];
			}
			if((sum != 3 && sum != -3) && colMill[3][0]){
					colMill[3][1] = false;
				}
			
			sum = 0;
			for(int i = 4; i < 7; i++){
				for(int j = 0; j < 7;j++){
					sum = sum + position[j][i];
				}
				if((sum != 3 && sum != -3) && colMill[i][0]){
					colMill[i][0] = false;
				}
				sum = 0;
			}
			return removePeg;
		}
	private boolean validMove(int newRow, int newCol, int oldRow, int oldCol){
		boolean validMove = false;
		if(getWhitePeg() == fly)return true;
		if(getBlackPeg() == fly)return true;
		if(newRow == oldRow){
			if(newRow == 0 || newRow == 6){
				if(Math.abs(newCol - oldCol)==3)validMove = true;
			}
			if(newRow == 1 || newRow == 5){
				if(Math.abs(newCol - oldCol)==2)validMove = true;
			}
			if(newRow == 2 || newRow == 3 || newRow == 4){
				if(Math.abs(newCol - oldCol) == 1)validMove = true;
			}
		}
		if(newCol == oldCol){
			if(newCol == 0 || newCol == 6){
				if(Math.abs(newRow - oldRow)==3)validMove = true;
			}
			if(newCol == 1 || newCol == 5){
				if(Math.abs(newRow - oldRow)==2)validMove = true;
			}
			if(newCol == 2 || newCol == 3 || newRow == 4){
				if(Math.abs(newRow - oldRow) == 1)validMove = true;
			}
		}
		return validMove;
	}

	//function places peg only if peg has not been placed before
	private void placePeg(MouseEvent event, int row, int col){
		//create a new peg every time, but only in the setup phase
		ImageIcon blackPeg = new ImageIcon("src/peg_black.png");
		ImageIcon whitePeg = new ImageIcon("src/peg_white.png");
		JLabel black = new JLabel(blackPeg);
		JLabel white = new JLabel(whitePeg);
		
		if(getRow() < 0 || getCol() < 0)
			System.out.println("please use a valid location");
		else if(getPlayer() == 1 && getPlaceFlag() && position[getRow()][getCol()] == 0){
			//place a black peg
			black.setBounds(event.getX()-(pegSize/2), event.getY()-(pegSize/2),pegSize, pegSize + 1);
			board.add(black);
			setRowCol(event);
			position[getRow()][getCol()] = -1;
			setPlaceFlag(false);
			setBlackPeg(getBlackPeg() + 1);
		}
		else if(getPlayer() == 2 && getPlaceFlag() && position[getRow()][getCol()] == 0){
			//place white peg
			white.setBounds(event.getX()-(pegSize/2), event.getY()-(pegSize/2),pegSize, pegSize + 1);
			board.add(white);
			setRowCol(event);
			position[getRow()][getCol()] = 1;
			setPlaceFlag(false);
			setWhitePeg(getWhitePeg() + 1);
		}
		if(!(getWhitePeg() < getMaxWhitePeg() || getBlackPeg() < getMaxBlackPeg())){setNextPlayer(getPlayer());setPhase(2);}
		repaint();
	}
	
	//function that will place button in the frame
	private void addButton(JButton button, String name, MouseListener listener){
		button = new JButton(name);
		button.setBounds(insets.right + getButtonOffsetX(), insets.top + getButtonNumber()*getButtonOffsetY(),
				getButtonDimX(), getButtonDimY());
		
		button.addMouseListener(listener);
		this.add(button);
		setButtonNumber(getButtonNumber() + 1);
			
		}
	private class UndoHandler implements MouseListener{
		public void mouseClicked(MouseEvent e) {

			repaint();
		}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}		
	}
	private class ResetHandler implements MouseListener{
		//when clicked, this players turn is over --> next players turn
		public void mouseClicked(MouseEvent e) {
				board.removeAll();
				initialPosition();
				initialMill();
				blackPegs = 0;
				whitePegs = 0;
				removePeg = false;
				setPhase(1);
				errorLabel.setText("Game Reset");
				repaint();
		}
		public void mouseEntered(MouseEvent e) {/*IDEA--> when mouse entered check if there is anything else to do*/}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
	}
	//TODO
	private class BoardHandler implements MouseListener, MouseMotionListener{
		public void mouseDragged(MouseEvent arg0) {
			
			if(getPhase() == 2 && drag){
				
				//drag and drop peg
				try{
					String compStr = ((JLabel)(board.getComponentAt(mouse.getX(),mouse.getY()))).getIcon().toString();
					if((board.getComponentAt(arg0.getX(),arg0.getY()).getWidth() == pegSize
						&& board.getComponentAt(arg0.getX(),arg0.getY()).getHeight() == pegSize + 1)
						&& ((JLabel)(board.getComponentAt(arg0.getX(),arg0.getY()))).getIcon().toString().compareTo(compStr) == 0){
					board.getComponentAt(arg0.getX(),arg0.getY()).setLocation(arg0.getX() - (pegSize/2),arg0.getY()-(pegSize/2));
					mouse = arg0;
					}
				}
				catch(NullPointerException e){
					
					}
			}
			repaint();
			
		}
		public void mouseMoved(MouseEvent arg0) {			
			//out1.setText("X: " + board.getComponentAt(arg0.getX(),arg0.getY()).getX() + " Y:" + board.getComponentAt(arg0.getX(),arg0.getY()).getY() + " mouse:" + mouse.getX() + " " + mouse.getY());
			
			repaint();
		}
		public void mouseClicked(MouseEvent arg0) {
			setRowCol(arg0);
			if(getPhase() == 1){
				//as long as we are in phase 1 keep placing new pegs on the board
				placePeg(arg0, getRow(), getCol());
				if(rowMillUpdate())errorLabel.setText("REMOVE A PEG");
				if(colMillUpdate())errorLabel.setText("REMOVE A PEG");
			}
			if(!getPlaceFlag() && getPhase() == 1 && !removePeg){
				//change the player after previous player has placed a peg
				setNextPlayer(getPlayer());
				//allow to have another peg place, this time for the next player
				setPlaceFlag(true);
			}
			if(removePeg){
				int componentCount = board.getComponentCount();
				setRowCol(arg0);
				if((colMill[getCol()][0] || colMill[getCol()][1]))errorLabel.setText("Cant remove a peg here, its a full mill");
				else if(rowMill[getRow()][0] || rowMill[getCol()][1])errorLabel.setText("Cant remove peg here, its a full mill");
				else if((!(getRow() < 0 || getCol() < 0 ) && (((position[getRow()][getCol()] == -1) && getPlayer() == 2)
						|| ((position[getRow()][getCol()] == 1) && getPlayer() == 1)))
					&& (board.getComponentAt(arg0.getX(),arg0.getY()).getWidth() == pegSize 
						&& board.getComponentAt(arg0.getX(),arg0.getY()).getHeight() == pegSize + 1))
					{
					board.remove(board.getComponentAt(arg0.getX(),arg0.getY()));
					if(!(board.getComponentCount() == componentCount)) {
						setRemovePeg(false);
						if(position[getRow()][getCol()] == -1){
							setBlackPeg(getBlackPeg()-1);
							maxWhitePegs--;
						}
						if(position[getRow()][getCol()] == 1){
							setWhitePeg(getWhitePeg()-1);
							maxBlackPegs--;
						}
						if(getWhitePeg()+(maxWhitePegs - getWhitePeg()) <= loose){
							//TODO make JOptionPane
							JOptionPane.showMessageDialog(null, "BLACK is the winner");
							setPhase(3);
						}
						if(getBlackPeg()+(maxBlackPegs - getWhitePeg()) <= loose){
							//TODO make JOPTIONPANE
							JOptionPane.showMessageDialog(null, "WHITE is the winner");
							setPhase(3);
						}
						position[getRow()][getCol()] = 0;
					}
				}
					else ;
				}
				else ;
						
//TODO remove testing
			for(int i = 0; i < 7; i++){
				for(int j = 0; j < 7; j++)
					value[i][j].setText(String.format("%d",position[i][j]));
				
			}
//TODO end testing			
		}
		public void mouseEntered(MouseEvent arg0) {}
		public void mouseExited(MouseEvent arg0) {}
		public void mousePressed(MouseEvent arg0) {
			lastPeg = 0;
			oldX = -1;
			oldY = -1;
			oldRow = -1;
			oldCol = -1;
			//only drag when there is the correct peg (set drag = true)
			setRowCol(arg0);
			if(getRow() < 0 || getCol() < 0){
				errorLabel.setText("PLEASE CHOOSE A VALID TARGET");
			}
			else if(!removePeg && getPhase() == 2 && ((position[getRow()][getCol()] == 1 && getPlayer() == 2)
					|| (position[getRow()][getCol()] == -1 && getPlayer() == 1))){
				oldX = board.getComponentAt(arg0.getX(),arg0.getY()).getX();
				oldY = board.getComponentAt(arg0.getX(),arg0.getY()).getY();
				
				oldRow = getRow();
				oldCol = getCol();
				drag = true;
				lastPeg = position[getRow()][getCol()];
				position[getRow()][getCol()] = 0;
				mouse = arg0;
			}
			for(int i = 0; i < 7; i++){
				for(int j = 0; j < 7; j++)
					value[i][j].setText(String.format("%d",position[i][j]));
				
			}
			repaint();
		}
		public void mouseReleased(MouseEvent arg0) {
			drag = false;
			setRowCol(arg0);
			try{
				if(oldRow < 0 || oldCol < 0){}
			else if(( (getRow() < 0 || getCol() < 0) && (board.getComponentAt(arg0.getX(),arg0.getY()).getWidth() == pegSize 
					&& board.getComponentAt(arg0.getX(),arg0.getY()).getHeight() == pegSize + 1))
					|| !(validMove(getRow(), getCol(), oldRow, oldCol))){
				errorLabel.setText("PLEASE CHOOSE A VALID TARGET");
				board.getComponentAt(arg0.getX(),arg0.getY()).setLocation(oldX,oldY);
				position[oldRow][oldCol] = lastPeg;
							
			}
			else if((getRow() < 0 || getCol() < 0) && !(board.getComponentAt(arg0.getX(),arg0.getY()).getWidth() == pegSize 
					&& board.getComponentAt(arg0.getX(),arg0.getY()).getHeight() == pegSize + 1)){}
				
			else if(getPhase() ==2 && position[getRow()][getCol()] == 0 
					&& (board.getComponentAt(arg0.getX(),arg0.getY()).getWidth() == pegSize 
					&& board.getComponentAt(arg0.getX(),arg0.getY()).getHeight() == pegSize + 1)
					&& (validMove(getRow(), getCol(), oldRow, oldCol))){
				board.getComponentAt(arg0.getX(),arg0.getY()).setLocation(arg0.getX() - (pegSize/2),arg0.getY()-(pegSize/2));
				position[getRow()][getCol()] = lastPeg;
				lastRow = getRow();
				lastCol = getCol();
				movePeg = true;
			}
			else;
			for(int i = 0; i < 7; i++){
				for(int j = 0; j < 7; j++)
					value[i][j].setText(String.format("%d",position[i][j]));
				
			}
			if(getPhase() == 2 && rowMillUpdate())errorLabel.setText("PLAYER" + getPlayer() + " please remove one opponents peg");
			else if(getPhase() == 2 && colMillUpdate())errorLabel.setText("PLAYER" + getPlayer() + " please remove one opponents peg");
			else if(getPhase() ==2 && movePeg){
				setNextPlayer(getPlayer());
				movePeg = false;
			}
			}catch(NullPointerException e){
				if(!(lastRow < 0 || lastCol < 0))position[lastRow][lastCol] = 0;
				board.getComponentAt(mouse.getX(),mouse.getY()).setLocation(oldX,oldY);
				if(!(oldRow < 0 || oldCol < 0))position[oldRow][oldCol] = lastPeg;
			}
			repaint();
		}		
	}
	//TODO
	private class LoadHandler implements MouseListener{
		public void mouseClicked(MouseEvent event){}
		public void mouseReleased(MouseEvent event){}
		public void mousePressed(MouseEvent event){}
		public void mouseEntered(MouseEvent event){}
		public void mouseExited(MouseEvent arg0) {}
		
	}
	//TODO
	private class SaveHandler implements MouseListener{
		public void mouseClicked(MouseEvent event){}
		public void mouseReleased(MouseEvent event){}
		public void mousePressed(MouseEvent event){}
		public void mouseEntered(MouseEvent event){}
		public void mouseExited(MouseEvent arg0) {}
		
	}
	//TODO
	private class ExitHandler implements MouseListener{
		public void mouseClicked(MouseEvent event){System.exit(0);}
		public void mouseReleased(MouseEvent event){}
		public void mousePressed(MouseEvent event){}
		public void mouseEntered(MouseEvent event){}
		public void mouseExited(MouseEvent arg0) {}
		
	}
	

	
	//get and set functions for buttons
	public int getButtonOffsetX(){return this.offsetX;}
	public int getButtonOffsetY(){return this.offsetY;}
	public int getButtonNumber(){return this.buttonNumber;}
	public int getButtonDimX(){return this.dimX;}
	public int getButtonDimY(){return this.dimY;}
	public int getTotalPegsAllowed(){return this.totalPegsAllowed;}
	public int getCount(){return this.count;}
	public int getPlayer(){return this.player;}
	public int getRow(){return this.row;}
	public int getCol(){return this.col;}
	public int getWhitePeg(){return this.whitePegs;}
	public int getBlackPeg(){return this.blackPegs;}
	public int getMaxBlackPeg(){return this.maxBlackPegs;}
	public int getMaxWhitePeg(){return this.maxWhitePegs;}
	public int getPhase(){return this.phase;}
	public boolean getPlaceFlag(){return this.placeFlag;}
	public boolean getRemovePeg(){return this.removePeg;}
	
	public void setPhase(int number){this.phase = number;}
	public void setRemovePeg(boolean value){this.removePeg = value;}
	public void setWhitePeg(int number){this.whitePegs = number;}
	public void setBlackPeg(int number){this.blackPegs = number;}
	public void setPlaceFlag(boolean value){this.placeFlag = value;}
	public void setCount(int count){this.count = count;}
	public void setButtonNumber(int number){this.buttonNumber = number;}
	public void setTotalPegsAllowed(int number){this.totalPegsAllowed = number;}
	public void setNextPlayer(int player){
		if(player == 1) this.player = 2;
		else if(player == 2) this.player = 1;
		else errorLabel.setText("ERROR, in setNextPlayer");
		nextPlayerLabel.setText("TURN: PLAYER " + getPlayer());
	}
	//this method will set rows and cols at the same time
	//always call this before getRow() and getCol()
	private void setRowCol(MouseEvent event) {
		//ATTENTION: indexed by 0 not 1 !!!
		this.col = -1;
		this.row = -1;
		// row 1
		if (event.getY() > 20 && event.getY() < 40) {
			this.row = 0;
			// position 1,1
			if (event.getX() > 20 && event.getX() < 40) {this.col = 0;}
			// position 1,2
			if (event.getX() > 210 && event.getX() < 230) {this.col = 3;}
			// position 1,3
			if (event.getX() > 390 && event.getX() < 410) {this.col = 6;}
		}

		// row 2
		if (event.getY() > 80 && event.getY() < 100) {
			this.row = 1;
			// position 2,1
			if (event.getX() > 80 && event.getX() < 100) {this.col = 1;}
			// position 2,2
			if (event.getX() > 210 && event.getX() < 230) {this.col = 3;}
			// position 2,3
			if (event.getX() > 335 && event.getX() < 355) {this.col = 5;}
		}
				
		// row 3
		if (event.getY() > 130 && event.getY() < 150) {
			this.row = 2;
			// position 3,1
			if (event.getX() > 130 && event.getX() < 150) {this.col = 2;}
			// position 3,2
			if (event.getX() > 210 && event.getX() < 230) {this.col = 3;}
			// position 3,3
			if (event.getX() > 280 && event.getX() < 300) {this.col = 4;}
			
		}

		// row 4 (6 pegs)
		if (event.getY() > 205 && event.getY() < 225) {
			this.row = 3;
			// position 4,1
			if (event.getX() > 20 && event.getX() < 40) {this.col = 0;}
			// position 4,2
			if (event.getX() > 80 && event.getX() < 100) {this.col = 1;}
			// position 4,3
			if (event.getX() > 130 && event.getX() < 150) {this.col = 2;}
			// position 4,5
			if (event.getX() > 285 && event.getX() < 305) {this.col = 4;}
			// position 4,6
			if (event.getX() > 335 && event.getX() < 355) {this.col = 5;}
			// position 4,7
			if (event.getX() > 390 && event.getX() < 410) {this.col = 6;}
			
		}

		// row 5
		if (event.getY() > 290 && event.getY() < 310) {
			this.row = 4;
			// position 5,1
			if (event.getX() > 130 && event.getX() < 150) {this.col = 2;}
			// position 5,2
			if (event.getX() > 210 && event.getX() < 230) {this.col = 3;}
			// position 5,3
			if (event.getX() > 280 && event.getX() < 300) {this.col = 4;}
		}

		// row 6
		if (event.getY() > 335 && event.getY() < 355) {
			this.row = 5;
			// position 6,1
			if (event.getX() > 80 && event.getX() < 100) {this.col = 1;}
			// position 6,2
			if (event.getX() > 210 && event.getX() < 230) {this.col = 3;}
			// position 6,3
			if (event.getX() > 335 && event.getX() < 355) {this.col = 5;}
		}

		// row 7
		if (event.getY() > 390 && event.getY() < 410){
			this.row = 6;
			// position 7,1
			if (event.getX() > 20 && event.getX() < 40) {this.col = 0;}
			// position 7,2
			if (event.getX() > 210 && event.getX() < 230) {this.col = 3;}
			// position 7,3
			if (event.getX() > 390 && event.getX() < 410) {this.col = 6;}		
		}
	}
}
